# # -*- encoding: utf-8 -*-
# # @Time       :  20:48
# # @Author     : yuxian
# # @Email      : 1503889663@qq.com
# # @File       : serializers.py
# # @SoftWare   : PyCharm
import re
import cn2an
from django.db.models import Q, QuerySet
from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer
from .models import SimilarCharacter
from book1.models import ImageData as ImageData1
from book2.models import ImageData as ImageData2
from book3.models import ImageData as ImageData3


class SimilarCharacterSerializer(ModelSerializer):
    class Meta:
        model = SimilarCharacter
        fields = '__all__'


class ParentSerializer(ModelSerializer):
    children = SerializerMethodField()

    class Meta:
        model = SimilarCharacter
        fields = "__all__"

    def get_children(self, char):
        data = SimilarCharacter.objects.filter(parent_id=char.id)
        return SimilarCharacterSerializer(data, many=True).data


class SimilarCharacterSerializerWithOriginImage(ModelSerializer):
    char_image_origin = SerializerMethodField()
    book_map = {'玄应音义': ImageData1, '慧琳音义': ImageData2, '希麟音义': ImageData3}

    class Meta:
        model = SimilarCharacter
        fields = '__all__'

    def utils(self, obj, ImageData):
        book, name, file_label = tuple(obj.image_path_label.split("/"))
        a = re.findall("第(.*?)卷", file_label)[0]
        if a.isdigit():
            number = int(a)
        else:
            number = cn2an.cn2an(a)
        try:
            data = ImageData.objects.get(name=book)
        except ImageData.DoesNotExist:
            return None
        version_list = ImageData.objects.filter(Q(parent_id=data.id))
        image_data_list = []
        for version in version_list:
            data = ImageData.objects.filter(Q(parent_id=version.id) & Q(name__startswith=name))
            version_data = []
            for i in data:
                for num in re.findall(f"{name}_(\d+?)-(\d+?)", i.name):
                    start, end = tuple(num)
                    if int(start) <= number <= int(end):
                        version_data.append(i.save_id)
            image_data_list.append([version.name, version_data])
        return image_data_list

    def get_char_image_origin(self, char):
        need_data = []
        if isinstance(char, QuerySet):
            for word_1 in char:
                book, name, file_label = tuple(word_1.image_path_label.split("/"))
                data = self.utils(word_1, self.book_map.get(book))
                if data:
                    need_data.extend(data)
        else:
            book, name, file_label = tuple(char.image_path_label.split("/"))
            data = self.utils(char, self.book_map.get(book))
            if data:
                need_data.extend(data)
        return need_data


class ParentSerializerWithOriginImage(ModelSerializer):
    children = SerializerMethodField()
    char_image_origin = SerializerMethodField()
    book_map = {'玄应音义': ImageData1, '慧琳音义': ImageData2, '希麟音义': ImageData3}

    class Meta:
        model = SimilarCharacter
        fields = "__all__"

    def get_children(self, char):
        data = SimilarCharacter.objects.filter(parent_id=char.id)
        return SimilarCharacterSerializerWithOriginImage(data, many=True).data

    def utils(self, obj, ImageData):
        book, name, file_label = tuple(obj.image_path_label.split("/"))
        a = re.findall("第(.*?)卷", file_label)[0]
        if a.isdigit():
            number = int(a)
        else:
            number = cn2an.cn2an(a)
        try:
            data = ImageData.objects.get(name=book)
        except ImageData.DoesNotExist:
            return None
        version_list = ImageData.objects.filter(Q(parent_id=data.id))
        image_data_list = []
        for version in version_list:
            data = ImageData.objects.filter(Q(parent_id=version.id) & Q(name__startswith=name))
            version_data = []
            for i in data:
                for num in re.findall(f"{name}_(\d+?)-(\d+?)", i.name):
                    start, end = tuple(num)
                    if int(start) <= number <= int(end):
                        version_data.append(i.save_id)
            image_data_list.append([version.name, version_data])
        return image_data_list

    def get_char_image_origin(self, char):
        need_data = []
        if isinstance(char, QuerySet):
            for word_1 in char:
                book, name, file_label = tuple(word_1.image_path_label.split("/"))
                data = self.utils(word_1, self.book_map.get(book))
                if data:
                    need_data.extend(data)
        else:
            book, name, file_label = tuple(char.image_path_label.split("/"))
            data = self.utils(char, self.book_map.get(book))
            if data:
                need_data.extend(data)
        return need_data
